const areaData = require('../../utils/areaData')
const {addressList} = require('../../apiConfig/apiList/luban')
const {getNearbySchools, userAddAddress} = require('../../apiConfig/apiList/luban')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      city: '',
      schoolId: '',
      detailAddress: '',
      phone: '',
    },
    latitude: '',
    longitude: '',
    addressList: [],
    schoolList: [],
    value1: 0,
    adress: '',
    showAddress : false,
    showAreaName : '',
    areaList: areaData.areaList,
    isEnd : false,
    params: {
      pageNum: 1,
      pageSize: 100,
      orderByColumn: 'create_time',
      isAsc: 'desc',
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAddress();
    this.getLocal();
  },

  getAddress(){
    const that = this;
    let list = this.data.addressList;
    let params = this.data.params;
    addressList(params).then(res=>{
      if(res.code == 200){
        let data = res.rows;
        this.setData({
          addressList: list.concat(data)
        });
        if(that.data.addressList.length < res.total){
          params.pageNum++;
        }else{
          this.setData({
            isEnd: 'ture'
          });
        }
      }
    })
  },
  getCity: function(e) {
    this.setData({
      'formData.city': e.detail.value
    })
  },
  getInputAddress: function(e) {
    console.log(344445, e)
    let list = this.data.addressList;
    let curIdx = e.currentTarget.dataset.index;
    list.forEach((item, index)=>{
      if(curIdx === index){
        item.detailAddress = e.detail.value;
      }
    })
    this.setData({
      'formData.detailAddress': e.detail.value
    })
  },
  getScholLd: function(e) {
    this.setData({
      'formData.schoolId': e.detail
    })
  },
  getPhone: function(e) {
    let list = this.data.addressList;
    let curIdx = e.currentTarget.dataset.index;
    list.forEach((item, index)=>{
      if(curIdx === index){
        item.phone = e.detail.value;
      }
    })
    this.setData({
      'formData.phone': e.detail.value
    })
  },
  onClickArea(){
    this.setData({
      showAddress : true
    })
  },
  selectArea(e){
    console.log(e.detail.values)
    let data = e.detail.values;
    let temp = []
    data.forEach(item=>{
      temp.push(item.name);
    })
    this.setData({
      showAddress : false,
      curAreaList: data,
      showAreaName: temp.join('/')
    })
  },
  closeArea(e){
    this.setData({
      showAddress : false
    })
  },
  onClose(e){
    console.log(234234234)
  },
  saveData(e){
    let data = e.currentTarget.dataset.item;
    let list = this.data.addressList;
    let curIdx = e.currentTarget.dataset.index;
    let params = {
      schoolId: data.schoolId,
      phone: '',
      detailAddress: '',
      id: data.id,

    };
    list.forEach((item, index)=>{
      if(curIdx === index){
        params.phone = item.phone;
        params.detailAddress = item.detailAddress;
      }
    })

    userAddAddress(params).then(res=>{
      if(res.code == 200){
      }
      console.log('saveDatares', res);
    })
  },
  // saveData(e){
  //   console.log(234234234)
  //   let pages = getCurrentPages();
  //   let currPage = pages[pages.length - 1];
  //   let prevPage = pages[pages.length - 2];
  //   currPage.setData({
  //     msg: newtitle
  //   })
  //   prevPage.setData({
  //     msg: newtitle
  //   })
  // },
  getLocal(){
    // const that = this
    // wx.getLocation({  
    //   type: 'wgs84', // 默认为 wgs84 的 gps 坐标，如果要返回直接给 openLocation 使用的火星坐标，可传入 'gcj02'  
    //   success: function(res) {  
    //     console.log('纬度: ' + res.latitude);  
    //     console.log('经度: ' + res.longitude);    
    //     that.setData({  
    //       latitude: res.latitude,  
    //       longitude: res.longitude  
    //     });
    //     let params = {
    //       longitude: res.longitude,
    //       latitude: res.latitude,
    //       schoolId: wx.getStorageSync('profile').schoolId || '',
    //     }
    //     getNearbySchools(params).then(res=>{
    //       console.log('getNearbySchools: ', res.data);
    //       let data = res.data;
    //       let list = [];
    //       data.forEach(item=>{
    //         list.push({text: item.schoolName, value: item.id});
    //       })
    //       that.setData({
    //         schoolList: list
    //       });
    //     })
    //   },  
    //   fail: function(err) {  
    //     console.error('获取经纬度失败: ', err);  
    //   }  
    // });  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.isEnd) {
      this.getAddress();
    }else{
      wx.showToast({
        title: '已加载全部',
        icon: 'none',
        duration: 1000,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})